package com.livew.framework.base.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.livew.framework.base.dao.BaseMapper;
import com.livew.framework.base.dao.SysUserMapper;
import com.livew.framework.base.model.SysUser;
import com.livew.framework.base.service.SysUserService;
import com.livew.framework.enums.EStatus;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.page.PagedBeanUtil;
import com.livew.framework.page.PagedResult;
import com.livew.framework.utils.XTListUtils;
import com.livew.framework.utils.XTStringUtils;

@Service("sysUserService")
public class SysUserServiceImpl extends BaseServiceImpl<SysUser> implements SysUserService {

	@Resource
	private SysUserMapper sysUserMapper;

	public SysUserServiceImpl(BaseMapper<SysUser> baseMapper) {
		super(baseMapper);
	}

	@Override
	public SysUser getUserByUsername(String username) {
		if (XTStringUtils.isEmpty(username)) {
			return null;
		}
		SysUser user = sysUserMapper.selectUserByUsername(username);
		return user;
	}

	
	@Override
	public void save(SysUser record) throws XTBusinessException {
		if(record==null||record.getId()!=null){
			throw new XTBusinessException("参数错误");
		}
		checkUsername(record.getUsername(), record.getId());
		checkPhone(record.getPhone(), record.getId());
		checkEmail(record.getEmail(), record.getId());
		super.save(record);
	}

	@Override
	public void updateByPrimaryKey(SysUser record) throws XTBusinessException {
		checkUsername(record.getUsername(), record.getId());
		checkPhone(record.getPhone(), record.getId());
		checkEmail(record.getEmail(), record.getId());
		super.updateByPrimaryKeySelective(record);
	}

	/**
	 * 用户名是否重复
	 * 
	 * @param username
	 * @param id
	 * @throws XTBusinessException
	 */

	public void checkUsername(String username, Long id) throws XTBusinessException {
		SysUser user = new SysUser();
		user.setId(id);
		user.setUsername(username);
		List<SysUser> oldList = getChcekInfoByCondition(user);
		if (!XTListUtils.isEmpty(oldList)) {
			throw new XTBusinessException("用户名重复");
		}
	}

	/**
	 * 用户名是否重复
	 * 
	 * @param username
	 * @param id
	 * @throws XTBusinessException
	 */
	public void checkPhone(String phone, Long id) throws XTBusinessException {
		SysUser user = new SysUser();
		user.setId(id);
		user.setPhone(phone);
		List<SysUser> oldList = getChcekInfoByCondition(user);
		if (!XTListUtils.isEmpty(oldList)) {
			throw new XTBusinessException("手机号重复");
		}
	}

	/**
	 * 邮箱
	 * 
	 * @param email
	 * @param id
	 * @throws XTBusinessException
	 */
	public void checkEmail(String email, Long id) throws XTBusinessException {
		SysUser user = new SysUser();
		user.setId(id);
		user.setEmail(email);
		List<SysUser> oldList = getChcekInfoByCondition(user);
		if (!XTListUtils.isEmpty(oldList)) {
			throw new XTBusinessException("邮箱重复");
		}
	}

	@Override
	public PagedResult<SysUser> findPagination(SysUser user) {
		Integer pageNo = user.getPageNo() == null ? 1 : user.getPageNo();
		Integer pageSize = user.getPageSize() == null ? 15 : user.getPageSize();
		PageHelper.startPage(pageNo, pageSize);
		List<SysUser> list = sysUserMapper.selectList(user);
		PagedResult<SysUser> page = PagedBeanUtil.toPagedResult(list);
		return page;
	}

	@Override
	public void updatePwd(SysUser user) throws XTBusinessException {
		if(user==null||user.getId()==null||XTStringUtils.isEmpty(user.getPassword())||XTStringUtils.isEmpty(user.getRemark3())){
			throw new XTBusinessException("参数错误");
		}
		SysUser old = sysUserMapper.selectByPrimaryKey(user.getId());
		if(old ==null){
			throw new XTBusinessException("参数错误");	
		}
		if(!old.getPassword().equals(user.getPassword())){
			throw new XTBusinessException("原始密码错误");		
		}
		if(user.getRemark3().equals(user.getPassword())){
			throw new XTBusinessException("新密码和原始密码一样");		
		}
	   SysUser newUser = new SysUser(); 
	   newUser.setPassword(user.getRemark3());
	   newUser.setId(user.getId());
	   super.updateByPrimaryKeySelective(newUser);
	}

	
	@Override
	public void updateStatus(SysUser t) throws XTBusinessException {
		if (t == null||t.getId()==null) {
			throw new XTBusinessException("参数错误");
		}
		SysUser user = new SysUser();
		user.setId(t.getId());
		SysUser old = getByPrimaryKey(t.getId());
		if (old == null) {
			throw new XTBusinessException("参数错误");
		}
		if(old.getStatus().equals(EStatus.active.getCode())){
			user.setStatus(EStatus.Disabled.getCode());
		}else{
			user.setStatus(EStatus.active.getCode());
		}
		super.updateStatus(user);
	}

	@Override
	public SysUser getUserByPhone(String phone) {
		
		return null;
	}

	@Override
	public SysUser getUserByEmail(String email) {
		
		return null;
	}

	@Override
	public SysUser getLogin(SysUser user) {
		List<SysUser> list = sysUserMapper.selectLogin(user);
		if(!XTListUtils.isEmpty(list)) {
			return list.get(0);
		}
		return null;
	}
	
	
	
}
